<?php

Yii::import('application.models._base.BaseProductInfo');


class ProductInfo extends BaseProductInfo
{
	
	public $keywords;
	public $category_id;
	public static function model($className=__CLASS__){
		return parent::model($className);
	}
	
	public function rules() {
		return array(
			array('created, modified, is_delete', 'numerical', 'integerOnly'=>true),
			array('title, tag, short_description', 'length', 'max'=>255),
		    array('title, tag, short_description', 'required'),
			array('description,keywords', 'safe'),
			array('id, title, tag, short_description, description, created, modified, is_delete,category_id', 'safe', 'on'=>'search'),
		);
	}

    public function behaviors(){
        return array(
    		'CTimestampBehavior' => array(
    			'class' => 'zii.behaviors.CTimestampBehavior',
    			'createAttribute' => 'created',
    			'updateAttribute' => 'modified',
    		)
    	);
    }
	
	public function relations() {
		return array(
			'category'=>array(self::MANY_MANY,'Category','product_category_relations(product_id,category_id)'),
		);
	}
	
	public function search() {
	    
		$criteria = new CDbCriteria;
		$criteria->together = true;
		$criteria->with = 'category';
		if(!empty($this->keywords)){
			$criteria->compare('t.title', $this->keywords, true);
		}      
		if ($this->category_id !== null){
		    $criteria->compare('category.id',$this->category_id);
		}
		$criteria->compare('t.is_delete', 0);
		return new CActiveDataProvider($this, array(
				'criteria' => $criteria,
		));
	}
}